package net.sxnic.item.action;

import java.util.HashSet;
import java.util.Set;

import net.sxinfo.core.dao.Page;
import net.sxinfo.core.dao.hibernate3.HibernateCriteria;
import net.sxinfo.core.dao.hibernate3.HibernateDaoUtils;
import net.sxinfo.core.dao.hibernate3.HibernateOrder;
import net.sxinfo.core.util.WebUtils;
import net.sxnic.item.mdata.action.MdataAction;
import net.sxnic.ugr.context.AppContext;
import net.sxnic.ugr.role.Role;
import net.sxnic.ugr.security.AuthorizationManager;
import net.sxnic.ugr.user.User;
import net.sxnic.ugr.user.UserManager;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

@SuppressWarnings("serial")
public class UserList extends MdataAction {
	
	private static String SYMBOL = "-";
	Page datas;
	@Autowired
	UserManager userManager;
	@Autowired
	AuthorizationManager authorizationManager;
	
	public String execute() throws Exception {
		
		User user = ucenterService.findUserByUserName(AppContext
				.getUserName(request));
		String userPrefix = user.getUserOrder()+SYMBOL;
		int pageNumber = WebUtils.getPageNumber(request);
		int pageSize = WebUtils.getPageSize(request);
		
		//设置排序（默认ID）
		HibernateOrder infoOrder = HibernateDaoUtils.createHibernateOrder(
				"userOrder", true);
		//设置查询条件
		HibernateCriteria hc = new HibernateCriteria().add(infoOrder);
		Set<Role> roles = new HashSet<Role>();
		Role role = authorizationManager.getRole("approver");
		roles.add(role);
		//按用户名和角色条件查询
		Criterion dc = null;
		dc = Restrictions.in("role", roles);
		dc = Restrictions.like("username", userPrefix+"%");
		hc.add(dc);	
		
		datas = userManager.getPageByCriteria(pageNumber, pageSize, hc);

		return SUCCESS;
	}
	public Page getDatas() {
		return datas;
	}
	public void setDatas(Page datas) {
		this.datas = datas;
	}
}
